disk=$((ls /dev | grep nvme0n1 \
     || ls /dev | grep sda \
     || ls /dev | grep mmcblk0) | head -1)

parted -a optimal /dev/$disk -s mklabel GPT mkpart primary 0% 1M mkpart primary 1M 1051M mkpart primary linux-swap 1051M 5147M mkpart primary ext4 5147M 100% set 1 bios_grub on
ocs-sr --batch -scr -e1 auto -e2 -j2 -k -r -p true restoredisk linux-x86_64-uefi $disk

if [ "$disk" = "nvme0n1" ]
then
  partition="nvme0n1p4"
  efipartition="nvme0n1p2"
elif [ "$disk" = "sda" ]
then
  partition="sda4"
  efipartition="sda2"
elif [ "$disk" = "mmcblk0" ]
then
  partition="mmcblk0p4"
  efipartition="mmcblk0p2"
fi

mount /dev/$partition /mnt
mount /dev/$efipartition /mnt/boot/efi
mount -t proc proc /mnt/proc
mount -t sysfs sys /mnt/sys
mount -o bind /dev /mnt/dev
mount -t devpts pts /mnt/dev/pts

chroot /mnt grub-install --target=i386-pc /dev/$disk
[ -d /sys/firmware/efi ] && chroot /mnt mount -t efivarfs efivarfs /sys/firmware/efi/vars && chroot /mnt grub-install --target=x86_64-efi --bootloader-id=Linux --efi-directory=/boot/efi --removable

umount /mnt/proc
umount /mnt/dev/pts
umount /mnt/dev
umount /mnt/sys/firmware/efi/vars
umount /mnt/sys
umount /mnt/boot/efi
umount /mnt
